const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    // 运行时模块
    const runtime_mod = b.createModule(.{
        .root_source_file = b.path("src/runtime/runtime.zig"),
    });

    // Counter 示例
    const counter_exe = b.addExecutable(.{
        .name = "counter",
        .root_module = b.createModule(.{
            .root_source_file = b.path("examples/counter.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "cz-runtime", .module = runtime_mod },
            },
        }),
    });
    b.installArtifact(counter_exe);

    // 运行 counter
    const run_counter = b.addRunArtifact(counter_exe);
    const run_step = b.step("run", "Run counter example");
    run_step.dependOn(&run_counter.step);

    // Chat room 示例
    const chat_exe = b.addExecutable(.{
        .name = "chat",
        .root_module = b.createModule(.{
            .root_source_file = b.path("examples/chat_room.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "cz-runtime", .module = runtime_mod },
            },
        }),
    });
    b.installArtifact(chat_exe);

    // 运行 chat
    const run_chat = b.addRunArtifact(chat_exe);
    const chat_step = b.step("chat", "Run chat room example");
    chat_step.dependOn(&run_chat.step);

    // Exokernel 模块（共享）
    const exo_mod = b.createModule(.{
        .root_source_file = b.path("src/exokernel/exokernel.zig"),
    });

    // Exokernel 示例
    const exo_exe = b.addExecutable(.{
        .name = "exokernel",
        .root_module = b.createModule(.{
            .root_source_file = b.path("examples/exokernel_demo.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "exokernel", .module = exo_mod },
            },
        }),
    });
    b.installArtifact(exo_exe);

    // 运行 exokernel
    const run_exo = b.addRunArtifact(exo_exe);
    const exo_step = b.step("exo", "Run exokernel demo");
    exo_step.dependOn(&run_exo.step);

    // DBOS 模块（依赖 exokernel）
    const dbos_mod = b.createModule(.{
        .root_source_file = b.path("src/dbos/mod.zig"),
        .imports = &.{
            .{ .name = "exokernel", .module = exo_mod },
        },
    });

    // DBOS 示例
    const dbos_exe = b.addExecutable(.{
        .name = "dbos",
        .root_module = b.createModule(.{
            .root_source_file = b.path("examples/dbos_demo.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "dbos", .module = dbos_mod },
            },
        }),
    });
    b.installArtifact(dbos_exe);

    // 运行 DBOS
    const run_dbos = b.addRunArtifact(dbos_exe);
    const dbos_step = b.step("dbos", "Run DBOS demo");
    dbos_step.dependOn(&run_dbos.step);

    // 测试
    const tests = b.addTest(.{
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/runtime/runtime.zig"),
            .target = target,
            .optimize = optimize,
        }),
    });
    
    const test_step = b.step("test", "Run tests");
    test_step.dependOn(&b.addRunArtifact(tests).step);
}
